package com.timothy.util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class SaxHelper<T> extends DefaultHandler {
    private List<Person> list;
    private Person p;
    String nameflag;


    public List<Person> getList() {
        return this.list;
    }

    @Override
    public void startDocument() throws SAXException {
        this.list = new ArrayList<Person>();
    }

    @Override
    public void endDocument() throws SAXException {
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if (qName.equals("person")) {
            p = new Person();
            p.setId(new Integer(attributes.getValue("id")));
        }
        this.nameflag=qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equals("person")) {
            if (p != null) {
                list.add(p);
            }
        }
        nameflag="";
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if(nameflag.equals("name")){
            p.setName(new String(ch,start,length));
        }else if(nameflag.equals("age")){
            p.setAge(new Integer(new String(ch,start,length)));
        }
        
    }
    

}
